package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import fact.view.DTO.MakeDTO;
import fact.view.DTO.NodeHierarchyDTO;
import fact.view.DTO.ScenarioDTO;
import fact.view.DTO.SegmentDTO;
import fact.view.model.MakeModelBean;
import fact.view.model.NodeHierarchyModelBean;
import fact.view.model.ScenarioModelBean;
import fact.view.model.SegmentModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;


@ManagedBean(name = "nodeHierarchyBean")
@ViewScoped
public class NodeHierarchyBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean opened = false;
	private boolean editOpened = false;
	private int segmentId;
	private String segmentName;
	
	private SelectItem[] dataTypeSelectList;
	private SelectItem[] nodeTypeSelectList;
	
	private SelectItem[] parentSelectList;

	
	@ManagedProperty(value="#{nodeHierarchyModelBean}")
	private NodeHierarchyModelBean nodeHierarchyModelBean;
	
	@ManagedProperty(value="#{segmentModelBean}")
	private SegmentModelBean segmentModelBean;
	
	@ManagedProperty(value="#{makeModelBean}")
	private MakeModelBean makeModelBean;
	
	@ManagedProperty(value="#{scenarioModelBean}")
	private ScenarioModelBean scenarioModelBean;
	
	
	ArrayList<MakeDTO> makeList=new  ArrayList<MakeDTO>();
	ArrayList<ScenarioDTO> scenarioList=new  ArrayList<ScenarioDTO>();

	
	ArrayList<NodeHierarchyDTO> nodeList=new  ArrayList<NodeHierarchyDTO>();

	private NodeHierarchyDTO node=new NodeHierarchyDTO();
	private NodeHierarchyDTO editNode=new NodeHierarchyDTO();
	
		 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);


	public ArrayList<NodeHierarchyDTO> getNodeList() {
		
	
		try{
			
			if(session.getAttribute("session_nodeList"+segmentId)==null)
			{
				nodeList=nodeHierarchyModelBean.getNodeList(this.segmentId);
				session.setAttribute("session_nodeList"+segmentId, nodeList);
			}
			else
				nodeList=(ArrayList<NodeHierarchyDTO>)session.getAttribute("session_nodeList"+segmentId);
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		
		return nodeList;
	}
	
	
	
	public String addNode(ActionEvent event) 
	{
		
		try{
			
			if(session.getAttribute("session_makeList")==null)
			{
				makeList=makeModelBean.getMakeList();
				session.setAttribute("session_makeList", makeList);
			}
			else
				makeList=(ArrayList<MakeDTO>)session.getAttribute("session_makeList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		try{
			
			if(session.getAttribute("session_scenarioList")==null)
			{
				scenarioList=scenarioModelBean.getScenarioList();
				session.setAttribute("session_scenarioList", scenarioList);
			}
			else
				scenarioList=(ArrayList<ScenarioDTO>)session.getAttribute("session_scenarioList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		
	
		
		
		opened=false;
		editOpened=false;
		
		if(this.node.getLabel()==null || this.node.getLabel().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:nodeName","Node Name cannot be empty.");
		}
		if(this.node.getDataType()==null || this.node.getDataType().equalsIgnoreCase("0"))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:dataType","Please Select Data Type.");
		}
		if(this.node.getNodeTypeId()==0)
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:nodeType","Please Select Node Type.");
		}
		
		if(this.node.getParentNode()==0 && this.node.getNodeTypeId()!=1)
		{
			opened=true;			
			FacesUtils.addErrorMessage("form:parentNode","Please Select Parent Node.");
		}
		
		
		if(opened==false)
		{
			node.setSegmentId(this.segmentId);
			try
			{
				
				if(nodeHierarchyModelBean.addNode(node,makeList,scenarioList)==true)
				{
					nodeList=nodeHierarchyModelBean.getNodeList(this.segmentId);
					session.setAttribute("session_nodeList"+segmentId, nodeList);
					
					FacesUtils.addInfoMessage(null,"Node Has Successfully been Added.");
					return "admin/user.jsf";
	
				}
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
				
			
		}
		
		return null;
	}
	
	public String deleteNode() throws SQLException
	{
		System.out.println("Node Id :"+this.node.getNodeId());
		if(nodeHierarchyModelBean.deleteNode(this.node.getNodeId())==true)
		{
			nodeList=nodeHierarchyModelBean.getNodeList(segmentId);
			session.setAttribute("session_nodeList"+segmentId, nodeList);
			
			FacesUtils.addInfoMessage(null,"Node has successfully been deleted.");
		}
		return "admin/nodeHierarchy.jsf";
	}
	
	public String editNode(ActionEvent event) 
	{
		
		
		opened=false;
		editOpened=false;
		
		if(this.editNode.getLabel()==null || this.editNode.getLabel().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:nodeName","Node Name cannot be empty.");
		}
		if(this.editNode.getDataType()==null || this.editNode.getDataType().equalsIgnoreCase("0"))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:dataType","Please Select Data Type.");
		}
		if(this.editNode.getNodeTypeId()==0)
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:nodeType","Please Select Node Type.");
		}
		
		if(this.editNode.getParentNode()==0 && this.editNode.getNodeTypeId()!=1)
		{
			editOpened=true;			
			FacesUtils.addErrorMessage("form:parentNode","Please Select Parent Node.");
		}
		
		
		
		if(editOpened==false)
		{
			editNode.setSegmentId(this.segmentId);
			try
			{
				if(nodeHierarchyModelBean.editNode(editNode)==true)
				{
					nodeList=nodeHierarchyModelBean.getNodeList(this.segmentId);
					session.setAttribute("session_nodeList"+segmentId, nodeList);
					
					FacesUtils.addInfoMessage(null,"Node Has Successfully been Added.");
					return "admin/user.jsf";
	
				}
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
				
			
		}
		
		return null;
	}

	public int getSegmentId() {
		return segmentId;
	}
	public void setSegmentId(int segmentId) {
		this.segmentId = segmentId;
	}
	
	public void toggleSegmentId(ValueChangeEvent e) 
	{
		try
		{
		this.segmentId=Integer.parseInt(e.getNewValue().toString());
		getNodeList();
		setSegmentName("");
		System.out.println("setting segmentId :"+segmentId);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void setNodeList(ArrayList<NodeHierarchyDTO> nodeList) {
		this.nodeList = nodeList;
	}

	public NodeHierarchyModelBean getNodeHierarchyModelBean() {
		return nodeHierarchyModelBean;
	}

	public void setNodeHierarchyModelBean(
			NodeHierarchyModelBean nodeHierarchyModelBean) {
		this.nodeHierarchyModelBean = nodeHierarchyModelBean;
	}



	public NodeHierarchyDTO getNode() {
		return node;
	}



	public void setNode(NodeHierarchyDTO node) {
		this.node = node;
	}



	public NodeHierarchyDTO getEditNode() {
		return editNode;
	}



	public void setEditNode(NodeHierarchyDTO editNode) {
		this.editNode = editNode;
	}



	public SelectItem[] getDataTypeSelectList() {

		dataTypeSelectList = new SelectItem[2];
		dataTypeSelectList[0] = new SelectItem("INTEGER", "INTEGER");
		dataTypeSelectList[1] = new SelectItem("NONE", "NONE");		
		
		return dataTypeSelectList;
	}




	public SelectItem[] getNodeTypeSelectList() {

		nodeTypeSelectList = new SelectItem[3];
		nodeTypeSelectList[0] = new SelectItem(1, "HEADER1");
		nodeTypeSelectList[1] = new SelectItem(2, "KPI");
		nodeTypeSelectList[2] = new SelectItem(4, "HEADER2");
		
		
		return this.nodeTypeSelectList;
	}
	
	


	public String getSegmentName() {
		return segmentName;
	}



	public void setSegmentName(String segmentName){
		
		try{
		SegmentDTO segment=segmentModelBean.getSegment(segmentId);
		this.segmentName = segment.getSegmentName();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}



	public SegmentModelBean getSegmentModelBean() {
		return segmentModelBean;
	}



	public void setSegmentModelBean(SegmentModelBean segmentModelBean) {
		this.segmentModelBean = segmentModelBean;
	}
	
	/** -- Popup Modal Event ---**/
	public boolean isOpened() { return opened; }
	public boolean isEditOpened() { return editOpened; }
    
    public void setOpened(boolean opened) { this.opened = opened; }
    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
    
    
    public void openEvent(ActionEvent event) {
    	//this.node=null;
        opened = true;
        editOpened = false;
    }
    public void openEditEvent(ActionEvent event){
    	try{
    	this.editNode=nodeHierarchyModelBean.getNode(editNode.getNodeId());
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    	editOpened = true;
    	opened = false;
    }
    
    
    public void closeEvent(ActionEvent event) {
        opened = false;
        editOpened = false;
    }

/*  End of Modal */
    
    public SelectItem[] getParentSelectList() {

		System.out.println("Segment Id ===>>"+this.segmentId);
		
		if(segmentId!=0)
		{
			System.out.println("Yes Segment id not zero ============================>>"+this.segmentId);
		if(nodeList!=null)
		{
			int counter=0;
			for(int i=0;i<nodeList.size();i++)
			{
				if(nodeList.get(i).getNodeTypeId()==1 || nodeList.get(i).getNodeTypeId()==4)
					counter++;
			}
		
			parentSelectList = new SelectItem[counter];
			
			
			counter=0;
		for(int i=0;i<nodeList.size();i++)
		{
			if(nodeList.get(i).getNodeTypeId()==1 || nodeList.get(i).getNodeTypeId()==4)
			{
				parentSelectList[counter] = new SelectItem(nodeList.get(i).getNodeId(), nodeList.get(i).getLabel());
				counter++;
			}
		}
		
		
		}
		}
		
		
		return this.parentSelectList;
	}



	public MakeModelBean getMakeModelBean() {
		return makeModelBean;
	}



	public void setMakeModelBean(MakeModelBean makeModelBean) {
		this.makeModelBean = makeModelBean;
	}



	public ScenarioModelBean getScenarioModelBean() {
		return scenarioModelBean;
	}



	public void setScenarioModelBean(ScenarioModelBean scenarioModelBean) {
		this.scenarioModelBean = scenarioModelBean;
	}
    
    

    
}